import { BaseHandler } from './BaseHandler';

export class RecorderHandler extends BaseHandler {

    /** 录屏组件实例 */
    recorder: any = null

    /** 是否在录制中 */
    recording: boolean = false

    /** 是否在停止中 */
    stoping: boolean = false

    /** 录制时长 */
    videoTime: number = -1

    /** 录屏文件路径 */
    videoPath: string = ''

    /** 目前未知 */
    getVideo: boolean = false

    init() {
        if (this.basePlatform.isDevtools) {
            return
        }
        if (this.basePlatform.platform) {
            this.recorder = this.basePlatform.platform.getGameRecorderManager()
            console.log('录屏组件加载完毕')
        }
    }

    /**
     * 开始录制
     * @param duration 持续时间，最小为5，最大为300
     * @returns 
     */
    start(duration = 280) {
        if (this.recorder == null) {
            return
        }
        var that: RecorderHandler = this
        this.recorder.onStart((res) => {
            console.log('start ' + this.videoPath)
            this.videoPath = ''
        })
        this.recorder.onStop((res) => {
            that.videoPath = res.videoPath
            that.getVideo = false
            if (that.videoTime > 0) {
                that.recorder.clipVideo({
                    path: res.videoPath,
                    timeRange: [3, 0],
                    success(r) {
                        that.videoPath = r.videoPath
                        if (that.getVideo == true) {

                        }
                    },
                    complete() {
                        that.stoping = false
                        if (that.recording) {
                            console.log("结束完成，刷新录制")
                            that.start()
                        }
                    }
                })
            } else {
                that.recorder.clipVideo({
                    path: res.videoPath,
                    timeRange: [120, 0],
                    success(r) {
                        that.videoPath = r.videoPath
                        console.log('clipVideo ' + that.videoPath)
                        if (that.getVideo == true) {

                        }
                    },
                    complete() {
                        that.stoping = false
                        if (that.recording) {
                            console.log("结束完成，刷新录制")
                            that.start()
                        }
                    },
                    fail(e) {
                        console.log(e)
                    }
                })
            }
        })
        this.recording = true;
        if (this.stoping) {
            console.log('录制开始被打断，正在剪切视频');
            return;
        }
        console.log('录制开始');
        this.videoTime = 0;
        this.recorder.start({
            duration,
        });
    }

    /** 结束录制 */
    stop() {
        if (this.recorder == null) {
            return
        }
        if (this.recording == false) {
            return
        }
        console.log('结束录制')
        this.recording = false
        this.stoping = true
        this.recorder.stop()
    }

    /** 暂停录制 */
    pause() {
        if (this.recorder == null) {
            return
        }
        this.recorder.pause()
    }

    /** 恢复录制 */
    resume() {
        if (this.recorder == null) {
            return
        }
        this.recorder.resume()
    }
}


